const inspirecloud = require('@byteinspire/api')
const { isString } = require('./is')

module.exports = {
  async setCache(key, val, exp) {
    if (isString(val)) {
      if (exp) {
        await inspirecloud.redis.set(key, val, 'EX', exp)
        return
      }
      await inspirecloud.redis.set(key, val)
      return
    }
    await inspirecloud.redis.set(key, JSON.stringify(val))
    return
  },
  async getCache(key) {
    return JSON.parse(await inspirecloud.redis.get(key))
  },
  async removeCache(key) {
    return await inspirecloud.redis.del(key)
  },
}
